import "./assets/main.css";
import { createApp } from "vue";
import { createPinia } from "pinia";

import App from "./App.vue";
import router from "./router";

import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import { parseTime, goBack } from "@/utils/flow/hsharpUtils";

//引入highlight依赖和语言
import "highlight.js/styles/stackoverflow-light.css";
import hljs from "highlight.js/lib/core";
import hljsVuePlugin from "@highlightjs/vue-plugin";
//按需引入语言
import xml from "highlight.js/lib/languages/xml";
import json from "highlight.js/lib/languages/json";
hljs.registerLanguage("json", json);
hljs.registerLanguage("xml", xml);

const app = createApp(App);

app.use(hljsVuePlugin);
app.use(ElementPlus);
app.use(createPinia());
app.use(router);

app.mount("#app");

// 全局方法挂载
app.config.globalProperties.parseTime = parseTime;
app.config.globalProperties.goBack = goBack;

// // 全局组件挂载
// import nodeWrap from '@/components/flow/nodeWrap.vue'
// app.component('nodeWrap', nodeWrap); //初始化组件
// import addNode from '@/components/flow/addNode.vue'
// app.component('addNode', addNode); //初始化组件

import * as ElementPlusIconsVue from "@element-plus/icons-vue";
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

let debounce = (fn, delay) => {
  var delay = delay || 100;
  var timer;
  return function () {
    var th = this;
    var args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(function () {
      timer = null;
      fn.apply(th, args);
    }, delay);
  };
};

app.directive("enterNumber", {
  mounted(el, { value = 100 }, vnode) {
    el = el.nodeName == "INPUT" ? el : el.children[0];
    var RegStr =
      value == 0
        ? `^[\\+\\-]?\\d+\\d{0,0}`
        : `^[\\+\\-]?\\d+\\.?\\d{0,${value}}`;
    el.addEventListener(
      "input",
      debounce(() => {
        el.value = el.value.match(new RegExp(RegStr, "g"));
        el.dispatchEvent(new Event("input"));
      }),
    );
  },
});
